/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import entities.Candidate;
import entities.Company;
import entities.Language;
import entities.PublicRelations;
import entities.UserConfiguration;
import facades.LanguageFacade;
import java.io.Serializable;
import java.util.Locale;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Event;
import javax.enterprise.event.Observes;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import qualifiers.CreatedEntityEvent;
import qualifiers.EditedEntityEvent;
import qualifiers.LocaleChanged;
import qualifiers.LoggedIn;

/**
 *
 * @author Alvaro
 */
@Named(value = "localeManagerBean")
@SessionScoped
public class LocaleManagerBean implements Serializable{
    @Inject
    private LanguageFacade languageFacade;
    @Inject
    @LocaleChanged
    Event<Language> localeChangedEvent;
//    @Inject
//    @LoggedIn
//    private Candidate candidate;
//    @Inject
//    @LoggedIn
//    private Company company;
    private Locale locale;

    /**
     * Creates a new instance of TranslationBean
     */
    public LocaleManagerBean() {
    }
    
//    @PostConstruct
//    public void initLocal(){
//       if(candidate !=null && candidate.getUser().getUserConfigurationList()!=null && !candidate.getUser().getUserConfigurationList().isEmpty()){
//            changeFacesLocale(candidate.getUser().getUserConfigurationList().get(0).getLanguage());
//       }
//       else if(company !=null && company.getIdUser().getUserConfigurationList()!=null && !company.getIdUser().getUserConfigurationList().isEmpty()){
//            changeFacesLocale(company.getIdUser().getUserConfigurationList().get(0).getLanguage());
//       }
//       else{
//            Language language=languageFacade.find(FacesContext.getCurrentInstance().getViewRoot().getLocale());
//            if(language==null){
//                language=languageFacade.getEnglish();
//            }
//            changeFacesLocale(language);
//        }
//    }
    
    public Locale getLocale() {
        if(locale==null){
            return FacesContext.getCurrentInstance().getViewRoot().getLocale();
        }
        else{
            return locale;
        }
    }

    public String getLanguage() {
        if(locale==null){
            locale= FacesContext.getCurrentInstance().getViewRoot().getLocale();
        }
       
        return locale.getLanguage();
    }
    
    public void changeFacesLocale(Language language){
        Locale _locale = new Locale(language.getCode());
        FacesContext.getCurrentInstance().getViewRoot().setLocale(_locale);
        locale=_locale;
        localeChangedEvent.fire(language);
    }
    
    public void onEditedUserConfiguration(@Observes @EditedEntityEvent UserConfiguration userConfiguration){
        changeFacesLocale(userConfiguration.getLanguage());
    }
    
    public void onCreatedUserConfiguration(@Observes @CreatedEntityEvent UserConfiguration userConfiguration){
        changeFacesLocale(userConfiguration.getLanguage());
    }
    
    public void onLoggedInCandidateConfiguration(@Observes @LoggedIn Candidate candidate){
        if (candidate.getUser().getUserConfiguration()!=null){
            changeFacesLocale(candidate.getUser().getUserConfiguration().getLanguage());
        }
    }
    
    public void onLoggedInCompanyConfiguration(@Observes @LoggedIn Company company){
        if (company.getIdUser().getUserConfiguration()!=null){
            changeFacesLocale(company.getIdUser().getUserConfiguration().getLanguage());
        }
    }
    
    public void onLoggedInPublicRelationsConfiguration(@Observes @LoggedIn PublicRelations publicRelations){
        if (publicRelations.getUser().getUserConfiguration()!=null){
            changeFacesLocale(publicRelations.getUser().getUserConfiguration().getLanguage());
        }
    }
}
